package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseController;
import com.cxf.windex.base.util.FileFilterTool;
import com.cxf.windex.biz.WeiContentsBiz;
import com.cxf.windex.biz.WeiLawyerBiz;
import com.cxf.windex.entity.WeiContents;
import com.cxf.windex.entity.WeiLawyer;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author chenxf
 * @date 3/8/2018
 * @description
 */
@Controller
@RequestMapping("lawyer")
public class WeiLawyerController extends
  BaseController<WeiLawyerBiz, WeiLawyer> {

  @Autowired
  WeiContentsBiz contentsBiz;


  @ApiOperation(value = "新增/编辑", notes = "新增编辑")
  @RequestMapping(value = "/edit", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse edit(WeiLawyer weiLawyer) {
    if (weiLawyer.getId() == null || weiLawyer.getId() == 0) {
      return new ObjectRestResponse().rel(true).msg("律师ID不能为空");
    }
    weiLawyer = baseBiz.selectById(weiLawyer.getId());
    WeiContents contents=new WeiContents();
    contents.setLayerName(weiLawyer.getName());
    long count=contentsBiz.selectCount(contents);
    if(count==0){
      weiLawyer.setArticleCount("无");
    }else{
      weiLawyer.setArticleCount(count+"");
    }

    return new ObjectRestResponse().rel(true).msg("查询成功").result(weiLawyer);
  }

  @ApiOperation(value = "查询律师列表", notes = "查询律师列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "orderStr", value = "排序 desc", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "name", value = "姓名", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "cert", value = "执业证号", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "expert", value = "专长", required = false, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<WeiLawyer> list(
      @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
      @RequestParam(defaultValue = "desc") String orderStr, String name,String mobile,String cert, String expert) {
    Example example = new Example(WeiLawyer.class);
    Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(name)) {
      criteria.andLike("name", "%" + name + "%");
    }
    if (StringUtils.isNotBlank(mobile)) {
      criteria.andLike("mobile", "%" + mobile + "%");
    }
    if (StringUtils.isNotBlank(cert)) {
      criteria.andLike("cert", "%" + cert + "%");
    }
    if (StringUtils.isNotBlank(expert)) {
      //String[] experts=expert.split(",");
      criteria.andLike("expert", "%" + expert + "%");
    }
    int count = baseBiz.selectCountByExample(example);
    PageHelper.startPage(offset, limit);
    PageHelper.orderBy("crt_time " + orderStr);

    return new TableResultResponse<WeiLawyer>(count,offset,limit,
        baseBiz.selectByExample(example));

  }

  @ApiOperation(value = "保存", notes = "保存")
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse save(WeiLawyer entity,@RequestParam("imgpicFile")MultipartFile imgpicFile,@RequestParam("weixinFile")MultipartFile weixinFile) {
    if(imgpicFile!=null){
      Map<String,String> savePcMap=saveFile(request,imgpicFile,"imgpic");
      if(savePcMap!=null){
        if(savePcMap.get("code").equals("0")){
          entity.setImgpic(savePcMap.get("saveUrl"));
        }else{
          return new ObjectRestResponse().rel(false).msg(savePcMap.get("msg"));
        }
      }
    }
    if(weixinFile!=null){
      Map<String,String> savePcMap=saveFile(request,weixinFile,"weixin");
      if(savePcMap!=null){
        if(savePcMap.get("code").equals("0")){
          entity.setWeixin(savePcMap.get("saveUrl"));
        }else{
          return new ObjectRestResponse().rel(false).msg(savePcMap.get("msg"));
        }
      }
    }
    if (entity.getId() == null) {
      baseBiz.insertSelective(entity);
    } else {
      baseBiz.updateSelectiveById(entity);
    }
    return new ObjectRestResponse().rel(true).msg("保存成功");
  }

}
